package cn.com.netedge.iot.frontend.datasave.utils;

import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import cn.com.netedge.iot.common.constants.MqConstants;
import cn.com.netedge.iot.common.utils.DateUtil;
import cn.com.netedge.iot.frontend.datasave.model.online.DataCOnlineModel;
import cn.com.netedge.iot.model.event.EventModelWithJson;

import java.util.Date;

public class RabbitMQSender {

    private final static String QUEUE_NAME = MqConstants.QUEUE_DATA_C_ONLINE_MESSAGE;

    public static void main(String[] args) {
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("172.18.100.14"); // RabbitMQ 服务所在地址
        factory.setPort(5672); // RabbitMQ 服务端口
        factory.setUsername("admin"); // 用户名
        factory.setPassword("admin"); // 密码

        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {

            // 声明队列
            channel.queueDeclare(QUEUE_NAME, true, false, false, null);
            for(int i=0;i< 5;i++) {
                DataCOnlineModel onlineModel = new DataCOnlineModel();
                onlineModel.setDeviceId(11);
                onlineModel.setIsOnline(0);
                onlineModel.setDeviceType(1);
                onlineModel.setIP("172.18.108.20");
                onlineModel.setOccurTime(DateUtil.DateToLongStr(new Date()));
                EventModelWithJson json = new EventModelWithJson();
                json.setJsonData(JSON.toJSONString(onlineModel));
                json.setDataType("DataCOnlineModel");
                String message = JSON.toJSONString(json);

                // 发送消息
                channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
                System.out.println(" [x] Sent '" + message + "'");
                Thread.sleep(1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
